var FrogDiv = function(param){
    var op = document.createElement("div");
    op.setAttribute("id", param.domId);
    parent.document.body.appendChild(op);

    var dom = parent.document.getElementById(param.domId);
    dom.style.position = 'absolute';
    dom.style.overflow = 'hidden';
    dom.style.backgroundColor = 'white';
    dom.style.backgroundColor = param.backColor || "white";
    dom.style.borderWidth = param.borderWidth || '1px';
    dom.style.borderColor = param.borderColor || "black";
    dom.style.borderStyle = param.borderStyle || "solid";
    dom.innerHTML = param.content;
    dom.style.display = 'none';

    parent.onresize = function () {
        dom.style.display = 'none';
    }

    if(param.intermediary != undefined && param.intermediary != null){
        parent[param.intermediary.intermediaryName] = param.intermediary.data;
    }

    var mouseListen = function(ev) {
        if(ev.target != mouseListen.unListenDom && ev.target.id != mouseListen.unListenId){

            function checkParent(tar){
                if(tar.parentNode == null) {
                    return false;
                }else if(tar.parentNode == mouseListen.unListenDom){
                    return true;
                }else{
                    return checkParent(tar.parentNode);
                }
            }
            if(!checkParent(ev.target)){
                mouseListen.refDom.style.display = 'none';
            }
        }
    }

    mouseListen.refDom = dom;
    mouseListen.unListenDom = document.getElementById(param.referenceId);
    mouseListen.unListenId = this.domId;

    document.getElementsByTagName("body")[0].onmousedown = parent.document.onmousedown = mouseListen;


    function getParentIndex(node) {
        if(node.parentNode == null){
            return 0;
        }else{
            var zindex = node.parentNode.style.zIndex;
            if(zindex != null && zindex != ''){
                return zindex;
            }else{
                return getParentIndex(node.parentNode);
            }
        }
    }

    var obj = {
        zIndex : getParentIndex(window.frameElement) + 1,
        domId : param.domId,
        referenceId : param.referenceId,
        open : function (option) {
            var direction = option.direction;   // 浮窗位置: up, down, left, right
            var fWidth = option.width || '100px';
            var fHeight = option.height || '100px';

            function position(){    // 获取指定window对象相对于页面的绝对位置
                var ifr = window.frameElement;
                var left = 0, top = 0;

                do {
                    left += ifr.offsetLeft || 0;
                    top += ifr.offsetTop || 0;
                    ifr = ifr.offsetParent;
                } while(ifr);

                return {
                    left: left,
                    top: top
                };
            }

            var mDom = parent.document.getElementById(this.domId);
            var reference = document.getElementById(this.referenceId);

            mDom.style.zIndex = this.zIndex + 1;
            var pos = position();
            var relativePos = reference.getBoundingClientRect();

            mDom.style.width = fWidth;
            mDom.style.height = fHeight;
            mDom.style.display = 'block';

            if(direction == 'up'){
                mDom.style.top = (pos.top + relativePos.top + relativePos.height + mDom.offsetHeight) + 'px';
                mDom.style.left = (pos.left + relativePos.left) + 'px';
            }else if(direction == 'left'){
                mDom.style.top = (pos.top + relativePos.top) + 'px';
                mDom.style.left = (pos.left + relativePos.left - mDom.offsetWidth) + 'px';
            }else if(direction == 'right'){
                mDom.style.top = (pos.top + relativePos.top) + 'px';
                mDom.style.left = (pos.left + relativePos.left + mDom.offsetWidth) + 'px';
            }else{
                mDom.style.top = (pos.top + relativePos.top + relativePos.height) + 'px';
                mDom.style.left = (pos.left + relativePos.left) + 'px';
            }
        },
        destroy : function () {
            var mDom = parent.document.getElementById(this.domId);
            mDom.style.display = 'none';
            mDom.innerHTML = '';
        },
        listenEvent : function (dom, eventName, fn) {
            var data = this;
            var mDom = parent.document.getElementById(this.domId);
            mDom[eventName] = function () {
                fn(data);
            }
        }
    };

    return obj;
};